{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Action3 语法树构建_递归算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "# 定语从句语法\n",
    "# 三引号输出时保持原来的格式。（这里就保留\\n）\n",
    "grammar = '''\n",
    "玩狗 => 行为  ， 结果 。\n",
    "行为 => 主语 动作 宾语 \n",
    "结果 => 主语 获得 效果\n",
    "主语 => 我 | 我的朋友 | 今天 | 白天 | 晚上 | 小孩\n",
    "动作 => 遛 | 踢 | 召唤 | 骑着\n",
    "宾语 => 哈士奇 | 柴犬 | 柯基 | 皮卡丘 | 拉布拉多\n",
    "获得 => 被它 | 让它 | 被狗 | 让狗 | \n",
    "效果 => 咬了| 遛了 | 玩了\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n玩狗 => 行为  ， 结果 。\\n行为 => 主语 动作 宾语 \\n结果 => 主语 获得 效果\\n主语 => 我 | 我的朋友 | 今天 | 白天 | 晚上 | 小孩\\n动作 => 遛 | 踢 | 召唤 | 骑着\\n宾语 => 哈士奇 | 柴犬 | 柯基 | 皮卡丘 | 拉布拉多\\n获得 => 被它 | 让它 | 被狗 | 让狗 | \\n效果 => 咬了| 遛了 | 玩了\\n'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grammar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建语法字典\n",
    "def getGramdict(grammar , linesplit = '\\n', gramsplit = '=>'):\n",
    "    result = {}\n",
    "    for line in grammar.split(linesplit):\n",
    "        if not line.strip(): # 如果为空则执行下一个循环一直到grammer没有了\n",
    "            continue\n",
    "        expr, statement = line.split(gramsplit)\n",
    "        result[expr.strip()] = [i.split() for i in statement.split('|')] # 有|的话就split加入字典，没有|就直接加入字典\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成句子\n",
    "def generate(gramdict , target , isEng = False):\n",
    "    if target not in gramdict:\n",
    "        return target\n",
    "    find = random.choice(gramdict[target])\n",
    "    blank = ''\n",
    "    if isEng: blank = ' '\n",
    "    return blank.join(generate(gramdict,t,isEng) for t in find)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'玩狗': [['行为', '，', '结果', '。']],\n",
       " '行为': [['主语', '动作', '宾语']],\n",
       " '结果': [['主语', '获得', '效果']],\n",
       " '主语': [['我'], ['我的朋友'], ['今天'], ['白天'], ['晚上'], ['小孩']],\n",
       " '动作': [['遛'], ['踢'], ['召唤'], ['骑着']],\n",
       " '宾语': [['哈士奇'], ['柴犬'], ['柯基'], ['皮卡丘'], ['拉布拉多']],\n",
       " '获得': [['被它'], ['让它'], ['被狗'], ['让狗'], []],\n",
       " '效果': [['咬了'], ['遛了'], ['玩了']]}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gramdict = getGramdict(grammar)\n",
    "gramdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['行为', '，', '结果', '。']]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gramdict['玩狗']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小孩骑着皮卡丘，今天被狗玩了。'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generate(gramdict,'玩狗')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我召唤拉布拉多，白天让狗咬了。'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generate(gramdict,'玩狗')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.0 64-bit ('Bi_env': venv)",
   "language": "python",
   "name": "python38064bitbienvvenvba07af95a1bb4b078aa8134bba84dff2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
